<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>{$title}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__LAYUIADMIN__/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__LAYUIADMIN__/style/admin.css" media="all">
    <link rel="stylesheet" href="__ADMIN__/dtree/dtree.css">
    <link rel="stylesheet" href="__ADMIN__/dtree/font/dtreefont.css">
    <script src="__LAYUIADMIN__/layui/layui.js"></script>
    <script src="__JS__/jquery-1.10.2.min.js"></script>
    <link rel="stylesheet" href="__ADMIN__/viewer/viewer.min.css">
    <script src="__ADMIN__/viewer/viewer.min.js"></script>
</head>
<body>
<script>
    //防止页面后退
    history.pushState(null, null, document.URL);
    window.addEventListener('popstate', function () {
        history.pushState(null, null, document.URL);
    });

    function reload_token($obj){
        $.ajax({
            type: "POST",
            url: "{:url('Common/refresh_token')}",
            dataType: "json",
            success: function(data){
                console.log(data);
                if ($obj){
                    $obj.val(data.data);
                } else{
                    $("input[name='__token__']").val(data.data);
                }
            }
        });
    }

    var tool_even = function (obj, data, reloadTable) {
        if (obj.event === 'del') {
            var del_url = "handle?act=del";
            if (data.del_url) {
                del_url = data.del_url;
            }
            layer.confirm('真的删除行么', function (index) {
                $.ajax({
                    url: del_url,
                    data: {id: data.id},
                    success: function (res) {
                        if (res.code == 0) {
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.alert(res.msg);
                        }
                    }
                });
            });
        }else if (obj.event === 'dels') {
            var del_url = "handle?act=dels";
            if (data.del_url) {
                del_url = data.del_url;
            }
            layer.confirm('真的删除行么', function (index) {
                $.ajax({
                    url: del_url,
                    data: {id: data.id},
                    success: function (res) {
                        if (res.code == 0) {
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.alert(res.msg);
                        }
                    }
                });
            });
        }else if (obj.event === 'del_apply') {
            var del_url = "handle?act=del_apply";
            if (data.del_url) {
                del_url = data.del_url;
            }
            layer.confirm('真的删除认领吗?', function (index) {
                $.ajax({
                    url: del_url,
                    data: {id: data.id},
                    success: function (res) {
                        if (res.code == 0) {
                            obj.del();
                            layer.close(index);
                        } else {
                            layer.alert(res.msg);
                        }
                    }
                });
            });
        } else if (obj.event === 'info') {
            var edit_url = "info?id=" + data.id;
            if (data.edit_url) {
                edit_url = data.edit_url;
            }
            layer.open({
                type: 2
                , title: '编辑'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        } else if (obj.event === 'sub_info') {
            var edit_url = "sub_info?id=" + data.id;
            layer.open({
                type: 2
                , title: '上报资料'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        layer.confirm('确定要上报吗？', {
                            btn: ['确认','取消'] //按钮
                        }, function(){
                            $.ajax({
                                url: form.form.action,
                                data: field,
                                type: 'POST',
                                success: function (res) {
                                    if (res.code == 0) {
                                        layer.alert(res.msg);
                                        layer.close(index); //关闭弹层
                                        reloadTable();
                                    } else {
                                        layer.alert(res.msg);
                                    }
                                }
                            });
                        }, function(){

                        });
                        //提交 Ajax 成功后，静态更新表格中的数据
                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'suc') {
            var edit_url = "suc?id=" + data.id;
            layer.open({
                type: 2
                , title: '编辑'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'sends') {
            var edit_url = "sends?id=" + data.id;
            layer.open({
                type: 2
                , title: '补发时长'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'edit') {
            var edit_url = "add?id=" + data.id;
            if (data.edit_url) {
                edit_url = data.edit_url;
            }
            layer.open({
                type: 2
                , title: '编辑'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'volunteer') {
            var edit_url = "/admin/volunteer/add?id=" + data.id;
            if (data.edit_url) {
                edit_url = data.edit_url;
            }
            layer.open({
                type: 2
                , title: '编辑'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'edits') {
            var edit_url = "edits?id=" + data.id;
            if (data.edit_url) {
                edit_url = data.edit_url;
            }
            layer.open({
                type: 2
                , title: '编辑'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'setting') {
            var edit_url = "setting?id=" + data.id;
            layer.open({
                type: 2
                , title: '编辑'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        } else if (obj.event === 'read') {
            var edit_url = "read?id=" + data.id;
            layer.open({
                type: 2
                , title: '编辑'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , yes: function (index, layero) {

                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'audit_site') {
            var edit_url = "audit_site?id=" + data.id;
            layer.open({
                type: 2
                , title: '审核'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段
                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'apply') {
            var edit_url = "apply?id=" + data.id;
            layer.open({
                type: 2
                , title: '报名列表'
                , content: edit_url
                , maxmin: true
                , area: ['100%', '100%']
                , shadeClose: true
                , scrollbar: false
                , yes: function (index, layero) {

                }
                , success: function (layero, index) {

                }
            });
        }else if (obj.event === 'view'){

            var edit_url = "add?id=" + data.id;
            if (data.edit_url) {
                edit_url = data.edit_url;
            }
            layer.open({
                type: 2
                , title: '查看'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
            });
        }else if (obj.event === 'audit'){

            var edit_url = "audit_info?id=" + data.id;
            layer.open({
                type: 2
                , title: '审核'
                , content: edit_url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (form) {
                        var field = form.field; //获取提交的字段

                        //提交 Ajax 成功后，静态更新表格中的数据
                        $.ajax({
                            url: form.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    layer.close(index); //关闭弹层
                                    reloadTable();
                                } else {
                                    layer.alert(res.msg);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
                , success: function (layero, index) {

                }
            });
        }
    }

    layui.use(['layer', 'form', 'table'], function () {
        var layer = layui.layer
            , form = layui.form
            , table = layui.table;

        form.on('switch(status_change)', function (data) {
            var field = data.elem.getAttribute('field');
            var id = data.elem.getAttribute('id');
            var table_name = data.elem.getAttribute('table_name');
            $.post("{:url('Common/update_status')}",{id:id,field:field,table_name:table_name},function (res){
                //alert(JSON.stringify(res));
            },'json');
        });

        //刷新表格
        var reloadTable = function () {
            table.reload('LAY-user-manage');

        }

        //监听工具条
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            tool_even(obj, data, reloadTable);

        });

    });

    //事件
    var active = {

        batchdel: function (url, table) {
            var checkStatus = table.checkStatus('LAY-user-manage')
                , checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            var temp = new Array();
            for (var i = 0; i<checkData.length; i++){
                temp[i] = checkData[i].id;
            }
            layer.confirm('确定删除吗？', function (index) {
                //执行 Ajax 后重载
                $.ajax({
                    url: 'handle?act=batch',
                    data: {'ids':temp},
                    type: 'POST',
                    success: function (res) {
                        if (res.code == 0) {
                            table.reload('LAY-user-manage');
                            layer.msg("批量删除成功");
                        } else {
                            layer.alert("批量删除失败");
                        }
                    }
                });
            });
        },
        examinedel: function (url, table) {
            var checkStatus = table.checkStatus('LAY-user-manage')
                , checkData = checkStatus.data; //得到选中的数据

            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            var temp = new Array();
            for (var i = 0; i<checkData.length; i++){
                temp[i] = checkData[i].id;
            }
            layer.confirm('确定审核通过吗？', function (index) {
                //执行 Ajax 后重载
                $.ajax({
                    url: 'handle?act=examine',
                    data: {'ids':temp},
                    type: 'POST',
                    success: function (res) {
                        if (res.code == 0) {
                            table.reload('LAY-user-manage');
                            layer.msg("批量审核成功");
                        } else {
                            layer.alert("批量审核失败");
                        }
                    }
                });
            });
        }
        , add: function (url, table) {
            layer.open({
                type: 2
                , title: '添加'
                , content: url
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    window.index1 = layer.load(0, {shade: [0.5, '#393D49'] });
                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            url: data.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.close(index1);
                                    layer.alert(res.msg);
                                    table.reload('LAY-user-manage'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.close(index1);
                                    layer.alert(res.msg);
                                    var obj = layero.find('iframe').contents().find("input[name='__token__']");
                                    reload_token(obj);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
            });
        }, edit : function (url, table) {
            layer.open({
                type: 2
                , title: '添加'
                , content: "{:url('Order/edit')}"
                , maxmin: true
                , area: ['80%', '90%']
                , shadeClose: true
                , scrollbar: false
                , btn: ['确定', '取消']
                , yes: function (index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index]
                        , submitID = 'LAY-user-front-submit'
                        , submit = layero.find('iframe').contents().find('#' + submitID);

                    //监听提交
                    iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            url: data.form.action,
                            data: field,
                            type: 'POST',
                            success: function (res) {
                                if (res.code == 0) {
                                    layer.alert(res.msg);
                                    table.reload('LAY-user-manage'); //数据刷新
                                    layer.close(index); //关闭弹层
                                } else {
                                    layer.alert(res.msg);
                                    var obj = layero.find('iframe').contents().find("input[name='__token__']");
                                    reload_token(obj);
                                }
                            }
                        });

                    });

                    submit.trigger('click');
                }
            });
        }
    };

</script>